package com.lwl.controller.scl;

import com.github.pagehelper.PageInfo;
import com.lwl.entity.LayData;
import com.lwl.entity.MyPage;
import com.lwl.entity.scl.TSclSub;
import com.lwl.service.scl.TSclSubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (TSclSub)表控制层
 *
 * @author makejava
 * @since 2022-10-28 18:08:46
 */
@Controller
@RequestMapping("tSclSub")
public class TSclSubController {
    /**
     * 服务对象
     */
    @Autowired
    private TSclSubService tSclSubService;

    /*
        查询SCL试题
     */
    @GetMapping("queryAll")
    @ResponseBody
    public LayData queryAll(MyPage myPage, TSclSub tSclSub){
        PageInfo pageInfo = tSclSubService.queryAll(myPage,tSclSub);
        return new LayData(pageInfo);
    }

    /*
        批量导入Excel中的数据
     */
    @RequestMapping("importExcel")
    @ResponseBody
    public LayData importExcel(MultipartFile file){
        Boolean i = tSclSubService.importExcel(file);
        return new LayData(i);
    }

    /*
        添加/修改题目
     */
    @PostMapping("addOrUpdataSCLTitle")
    @ResponseBody
    public LayData addOrUpdataSCLTitle(TSclSub tSclSub){
        Boolean i = tSclSubService.addOrUpdataSCLTitle(tSclSub);
        return new LayData(i);
    }

    /*
        删除题目
     */
    @DeleteMapping("delSclTitle/{sclnumber}")
    @ResponseBody
    public LayData delSclTitle(@PathVariable Integer sclnumber){
        Boolean i = tSclSubService.delSclTitle(sclnumber);
        return new LayData(i);
    }

    /*
        批量删除题目（做过的不删）
     */
    @PostMapping("delSclnumbers/{sclnumbers}")
    @ResponseBody
    public LayData delSclnumbers(@PathVariable Integer[] sclnumbers){
        List<Integer> nodellist = tSclSubService.delSclnumbers(sclnumbers);
        return new LayData(nodellist);
    }

    /*
        添加前检查编号是否存在
     */
    @PostMapping("{sclnumber}")
    @ResponseBody
    public LayData checkSclNumber(@PathVariable Integer sclnumber){
        System.out.println("sclnumber = " + sclnumber);
        Boolean i = tSclSubService.checkSclNumber(sclnumber);
        return new LayData(i);
    }

    /*
        将数据传到主页面上
     */
    @GetMapping("index")
    public String getIndexAllSub(MyPage myPage, HttpServletRequest request){
        myPage.setLimit(90);
        myPage.setPage(1);

        PageInfo pageInfo = tSclSubService.getIndexAllSub(myPage);
        request.setAttribute("allsub",pageInfo);

        return "/scl/index";
    }
}
